package com.uenpay.im.ui.old.activity.crowdfund;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 众筹详情（江湖救急）
 * 
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GetCrowdfundInfoInvokItem extends HttpInvokeItem {

	public class GetCrowdfundInfoInvokItemResult {
		public int status;
		public String msg;
		public int fundid;
		public String custid;
		public String chatid;
		public String amount;
		public String fundamount;
		public String memo;
		public int sosstatus;
		public String createtime;
		public String nickname;
		public ArrayList<CFRecordEntity> arrayList = new ArrayList<CFRecordEntity>();
	}

	public GetCrowdfundInfoInvokItemResult getOutput() {
		return (GetCrowdfundInfoInvokItemResult) GetResultObject();
	}

	/**
	 * 众筹详情（江湖救急）
	 * 
	 * @param fundid
	 */
	public GetCrowdfundInfoInvokItem(String fundid) {
		String url = APIConfiguration.getBaseUrl()
				+ "Purse/Crowdfund/crowdfundInfo?fundid=" + fundid + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		GetCrowdfundInfoInvokItemResult result = new GetCrowdfundInfoInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject jsonData = json.optJSONObject("data");
			if (jsonData != null) {
				result.custid = jsonData.optString("custid");
				result.nickname = jsonData.optString("nickname");
				result.memo = jsonData.optString("memo");
				result.fundamount = jsonData.optString("fundamount");
				result.amount = jsonData.optString("amount");
				result.chatid = jsonData.optString("chatid");
				result.amount = jsonData.optString("amount");
				result.createtime = jsonData.optString("createtime");
				result.fundid = jsonData.optInt("fundid");
				result.sosstatus = jsonData.optInt("sosstatus");
				if (jsonData.has("list")) {
					JSONArray jsonArray = jsonData.optJSONArray("list");
					if (jsonArray != null && jsonArray.length() > 0) {
						for (int i = 0, size = jsonArray.length(); i < size; i++) {
							result.arrayList.add(desCFRecordEntity(jsonArray
									.optJSONObject(i)));
						}
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

	private CFRecordEntity desCFRecordEntity(JSONObject json) {
		CFRecordEntity mCFRecordEntity = new CFRecordEntity();
		mCFRecordEntity.id = json.optInt("id");
		mCFRecordEntity.fundid = json.optInt("fundid");
		mCFRecordEntity.custid = json.optString("custid");
		mCFRecordEntity.amount = json.optString("amount");
		mCFRecordEntity.createtime = json.optString("createtime");
		mCFRecordEntity.nickname = json.optString("nickname");
		mCFRecordEntity.ismax = json.optInt("ismax");
		return mCFRecordEntity;
	}
}
